package com.sohoappspot.sohosched.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.sohoappspot.sohosched.client.ContentWidget;
import com.sohoappspot.sohosched.client.SohoConstants;
import com.sohoappspot.sohosched.client.events.NavigationEvent;
import com.sohoappspot.sohosched.client.events.NavigationHandler;
import com.sohoappspot.sohosched.client.events.NavigationEvent.NavigationEventType;
import com.sohoappspot.sohosched.client.images.SohoImages;
import com.sohoappspot.sohosched.client.model.Model;

public class LeftHandMenu extends Composite {

	private static LeftHandMenuUiBinder uiBinder = GWT
			.create(LeftHandMenuUiBinder.class);

	public static final SohoImages images = (SohoImages) GWT
			.create(SohoImages.class);

	interface LeftHandMenuUiBinder extends UiBinder<Widget, LeftHandMenu> {
	}
	
	private final HandlerManager handlerManager = new HandlerManager(this);

	public LeftHandMenu(Model model, SohoConstants constants) {
		initWidget(uiBinder.createAndBindUi(this));
		setupMainMenu(constants);
	}

	@UiField
	Tree mainMenu;

	@UiHandler("mainMenu")
	public void onSelection(SelectionEvent<TreeItem> event) {
		TreeItem item = event.getSelectedItem();
		
		//Get View Mode From The User Object
		SohoConstants.ViewMode viewMode = (SohoConstants.ViewMode)item.getUserObject();
				
		NavigationEvent navEvent = new NavigationEvent(NavigationEventType.Changed, viewMode, item);
		handlerManager.fireEvent(navEvent);
	}
	
	 public void addNavigationHandler(NavigationHandler handler) {
		  handlerManager.addHandler(NavigationEvent.TYPE,handler);  
	 }

	public Tree getMainMenu() {
		return mainMenu;
	}

	private void setupMainMenu(SohoConstants constants) {
		// TreeItem menuApplication =
		// mainMenu.addItem(constants.menuApplication());
		// menuApplication.setState(false, false);
		
		AddTreeItem(constants.menuCalendar(), SohoConstants.ViewMode.Calendar);
		AddTreeItem(constants.menuCustomers(), SohoConstants.ViewMode.Customer);
		
		/*
		 * setupMainMenuOption(mainMenu, new LandingPageHeader(constants),
		 * images .catWidgets());
		 */
	}
	
	private void AddTreeItem(String navigationName, SohoConstants.ViewMode viewMode){
		
		TreeItem newItem = mainMenu.addItem(navigationName);
		newItem.setUserObject(viewMode);
		
		//This wasn't fired on setup so ignored
		//NavigationEvent navEvent = new NavigationEvent(NavigationEventType.Init, newItem);
		//handlerManager.fireEvent(navEvent);

	}

	/**
	 * Add an option to the main menu.
	 * 
	 * @param parent
	 *            the {@link TreeItem} that is the option
	 * @param content
	 *            the {@link ContentWidget} to display when selected
	 * @param image
	 *            the icon to display next to the {@link TreeItem}
	 */
	private void setupMainMenuOption(Tree parent, Composite content,
			ImageResource image) {
		// Create the TreeItem
		// TODO fix getClass().getName()
		TreeItem option = parent.addItem(AbstractImagePrototype.create(image)
				.getHTML()
				+ " " + content.getClass().getName());

		// Map the item to its history token and content widget
		// ToDO
		// itemWidgets.put(option, content);

		// TODO
		// itemTokens.put(getContentWidgetToken(content), option);
	}

}
